package org.harlov.testsys.views;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.harlov.testsys.loger;
import org.harlov.testsys.db.diffs;
import org.harlov.testsys.db.test_loger;
import org.harlov.testsys.db.tests;
import org.harlov.testsys.db.themes;
import org.harlov.testsys.db.users;

public class stest_form extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private gui_factory gFactory;
	private String login;
	private JFrame mFrame;
	private int prev_quest ;
	private int test_id ; //Выбранный тест
	private int test_list_id ;
	private int user_id;
	private int time_av = 0;
	private int time_past = 0;
	private Timer timer;
	public stest_form (String _login) {
		
		super("HTest|Режим тестирования|"+_login);
		gFactory = new gui_factory(this);
		login = _login;
		user_id = users.getId(_login);
		mFrame = this;
		this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} });
		
		this.setPreferredSize(new Dimension(800,600));
		this.setSize(800,600);
		this.getContentPane().setLayout(null);
		this.setResizable(false);
		gui_factory.setCenterPosition(this);
		this.setVisible(true);
		
		//USER INTERFACE
		//***********
		gFactory.newLabel("Выберите тему тестирования", "l_select themes",20, 200,250,20);
		gFactory.newComboBox("cb_themes", themes.getThemes(), 240,200,new theme_select());
		gFactory.newLabel("Выберите уровень сложности", "l_select_diff", 20, 240,240,20);
		gFactory.newComboBox("cb_difss", diffs.getDiffs(), 240,240, new difss_select());
		gFactory.newLabel("Выберите тест", "l_select_test", 500, 30,150,20);
		gFactory.newList("tests_list", tests.getTestsView(gFactory.getSelCombo("cb_themes"),gFactory.getSelCombo("cb_difss")) , 500, 50,200,500,new list_click());
		gFactory.setSelList("tests_list", 0);
		test_list_id = 1;
		test_id = tests.getTestId(test_list_id);
		gFactory.newButton("Начать тестирование", "btn_start_test", 200, 500, new start_test());
		
	}
	
	class theme_select implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			tests.getTestsView(gFactory.getSelCombo("cb_themes"),gFactory.getSelCombo("cb_difss"));
			
		}
		
	}
	
	class difss_select implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			tests.getTestsView(gFactory.getSelCombo("cb_themes"),gFactory.getSelCombo("cb_difss"));
			
		}
		
	}
	
	class list_click implements ListSelectionListener {

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
		if (gFactory.getSelList("tests_list") != 0) {
			System.out.println("Value changed");
			test_list_id = gFactory.getSelList("tests_list");
			test_id = tests.getTestId(test_list_id);
			loger.add("Selected test:>"+test_id,this, 0);
		}
		}
		
	}
	
	class start_test implements ActionListener {
	@Override
		public void actionPerformed(ActionEvent arg0) {
		
		//Интерфейс для тестирования
		//***********
			//Убираем панельки выбора теста
			gFactory.setVisibleElem("tests_list", false);
			gFactory.setVisibleElem("cb_themes", false);
			gFactory.setVisibleElem("cb_difss", false);
			gFactory.setVisibleElem("l_select themes", false);
			gFactory.setVisibleElem("l_select_diff",false);
			gFactory.setVisibleElem("l_select_test", false);
			gFactory.setVisibleElem("btn_start_test", false);
			mFrame.repaint();
			
			//Создаем UI student
			System.out.println(test_list_id);
			System.out.println(test_id);
			test_loger.StartNewTest(test_id,test_list_id,user_id);
			gFactory.newPanel("ans_panel", 210,270,400,270);
			gFactory.newTextArea("ta_quest",210, 50,570,200);
			gFactory.newList("quests_list",test_loger.getQuestsViewForStudents() ,0,50,new quest_click());
			gFactory.setSelList("quests_list", 0);
			prev_quest = 1;
			gFactory.newProgressBar("pb_time",tests.getTestTime(test_list_id)*60,0,0, 550, 800, 50);
			gFactory.newLabel("Вопрос", "l_quest", 220, 30,100,20);
			gFactory.newButton("Ответить","btn_anss", 620, 270, 100,50,new save_button_click());
			gFactory.newButton("Закончить тест","btn_end_test" , 620, 330, 100,50,new end_test());
			time_av = tests.getTestTime(test_list_id)*60;
			time_past = 0;
			mFrame.setTitle(tests.getTestName(test_list_id)+"|осталось:"+Integer.toString(time_av/60)+" минут");
			
			timer = new Timer(1000,new on_timer()); //Запускаем отсчет времени
			timer.start();
		}
	}
	
	class on_timer implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			time_past++;
			mFrame.setTitle(tests.getTestName(test_list_id)+"|осталось:"+Integer.toString((time_av-time_past)/60)+" минут");
			gFactory.setValueBar("pb_time", time_past);
			if (time_past == time_av) {
				gFactory.setVisibleElem("ans_panel",false);
				gFactory.setVisibleElem("ta_quest",false);
				gFactory.setVisibleElem("quests_list",false);
				gFactory.setVisibleElem("l_quest",false);
				gFactory.setVisibleElem("btn_anss",false);
				gFactory.setVisibleElem("btn_end_test",false);
				JOptionPane.showMessageDialog(null,"Время вышло!","Внимание",JOptionPane.OK_OPTION);
				timer.stop();
				test_loger.endTest();
				showResults();
			}
			
		}
		
	}
	
	private void showResults() {
		gFactory.newLabel("Правильно отвечено вопросов: "+test_loger.getResultForStud(), "l_right_result", 100, 500,400,50);	
	}
	
	class quest_click implements ListSelectionListener {
		
		@Override
		public void valueChanged(ListSelectionEvent arg0) {
		if (prev_quest!=gFactory.getSelList("quests_list")) {
			gFactory.ClearPanel("ans_panel");
			System.out.println("####Загрузка вопроса> "+gFactory.getSelList("quests_list"));
			test_loger.drawAns(gFactory,gFactory.getSelList("quests_list"));
			gFactory.SetTextToArea(test_loger.getQuestText(gFactory.getSelList("quests_list")),"ta_quest");
			gFactory.RevalidatePanel("ans_panel");
			prev_quest = gFactory.getSelList("quests_list");
			mFrame.repaint();
		}
			
		}
	}
	
	class save_button_click implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			test_loger.saveAns();
			gFactory.nextSelList("quests_list");			
		}
		
	}
	
	class end_test implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			gFactory.setVisibleElem("ans_panel",false);
			gFactory.setVisibleElem("ta_quest",false);
			gFactory.setVisibleElem("quests_list",false);
			gFactory.setVisibleElem("l_quest",false);
			gFactory.setVisibleElem("btn_anss",false);
			gFactory.setVisibleElem("btn_end_test",false);
			timer.stop();
			test_loger.endTest();
			showResults();
		}
		
	}
	
	
	
	

	

}
